package com.ghh.controller;
/*
*
* 角色管理
* */

import com.alibaba.dubbo.config.annotation.Reference;
import com.ghh.constant.MessageConstant;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.entity.Result;
import com.ghh.pojo.Role;
import com.ghh.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //查询分页操作
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return roleService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    //添加
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role){
        try {
            roleService.add(permissionIds,menuIds,role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //根据ID查询角色信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
          Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据id查询关联的权限id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //修改
    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role){
        try {
            roleService.edit(permissionIds,menuIds,role);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
